// $(function () {

// findAll(1, 10);
// });
layui.use(['form', 'laypage', 'layer', 'laydate'], function () {
    var laypage = layui.laypage,
        form = layui.form
        , layer = layui.layer
        , laydate = layui.laydate;

    //日期
    laydate.render({
        elem: '#start',
        trigger: 'click'
    });
    //日期
    laydate.render({
        elem: '#end',
        trigger: 'click'
    });
    // //监听提交
    form.on('submit(sreach)',
        function (data) {
            console.log(data);
            data = data.field;
            let arr_box = [];
            $('input[type=checkbox][name=hobby]:checked').each(function () {
                arr_box.push($(this).attr("title"));
            });
            data.hobby = arr_box.toLocaleString();
            //发异步，把数据提交给php
            page(data);
            return false;
        });
    //完整功能
    page({});
});

function page(data) {
    layui.use(['laypage'], function () {
        var laypage = layui.laypage;

        //完整功能
        laypage.render({
            elem: 'page'
            , count: getCount(data)
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            , jump: function (obj) {
                console.log(obj)
                findAll(obj.curr, obj.limit, data);
            }
        });
    });
}

function getCount(data) {
    let count = null;
    $.ajax({
        url: '/back/user/getCount',
        data: data,
        type: 'get',
        async: false,
        dataType: 'json',
        success: function (res) {
            count = res.count;
        }
    });
    return count;
}

function findAll(curr, limit, data) {
    // let data = {username: 'a'};
    let page = {pageNo: curr, pageSize: limit};
    // data.pageNo = curr;
    // data.pageSize = limit;
    $.ajax({
        url: '/back/user/findAll',
        data: Object.assign(data, page),
        type: 'get',
        dataType: 'json',
        success: function (res) {
            console.log(res);
            if (res.code==-1){
                window.open("/html/login.html", "_top");
                return;
            }
            let data = res.data;
            let html = '';
            for (let i = 0; i < data.length; i++) {
                html += ' <tr>\n' +
                    '                                    <td>' + data[i].id + '</td>\n' +
                    '                                    <td>' + data[i].username + '</td>\n' +
                    '                                    <td>' + data[i].sex + '</td>\n' +
                    '                                    <td>' + data[i].phone + '</td>\n' +
                    '                                    <td>' + (data[i].address == undefined ? '' : data[i].address) + '</td>\n' +
                    '                                    <td>' + (data[i].hobby == undefined ? '' : data[i].hobby) + '</td>\n' +
                    '                                    <td>' + data[i].birthday + '</td>\n' +
                    '                                    <td class="td-status">\n' +
                    '                                      <span class="layui-btn layui-btn-normal layui-btn-mini ' + (data[i].enable == 1 ? '' : 'layui-btn-disabled') + '">' + (data[i].enable == 1 ? '已启用' : '已停用') + '</span></td>\n' +
                    '                                    <td class="td-manage">\n' +
                    '                                      <a onclick="member_stop(this,' + data[i].id + ',' + data[i].enable + ')" href="javascript:;"  title="' + (data[i].enable == 1 ? '启用' : '停用') + '">\n' +
                    '                                        <i class="layui-icon">' + (data[i].enable == 1 ? '&#xe601;' : '&#xe62f;') + '</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a title="编辑"  onclick="edit(' + data[i].id + ')" href="javascript:;">\n' +
                    '                                        <i class="layui-icon">&#xe642;</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a onclick="editPassword(\'' + data[i].username + '\',' + data[i].id + ')" title="修改密码" href="javascript:;">\n' +
                    '                                        <i class="layui-icon">&#xe631;</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a title="删除" onclick="del(this,' + data[i].id + ')" href="javascript:;">\n' +
                    '                                        <i class="layui-icon">&#xe640;</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a title="蛋糕"  href="javascript:;">\n' +
                    '                                       <i class="iconfont" style="color: red">&#xe64d;</i>\n' +
                    '                                      </a>\n' +
                    '                                    </td>\n' +
                    '</tr>';
            }
            $("#tbody").html(html);
        }
    });

}

function editPassword(username, id) {
    sessionStorage.setItem("username", username);
    sessionStorage.setItem("userId", id);
    xadmin.open('修改密码', 'member-password.html', 600, 400);
}

function del(obj, id) {
    layer.confirm('确认要删除吗？', function (index) {
        //发异步删除数据
        //也需要判断当前用户是否已经登录
        isLogin();
        $.ajax({
            url: '/back/user/delete',
            data: {id: id},
            type: 'post',
            dataType: 'json',
            success: function (data) {
                if (data.msg == 'success') {
                    $(obj).parents("tr").remove();
                    layer.msg('已删除!', {icon: 1, time: 1000});
                } else {
                    layer.msg("删除用户失败");
                }
            }
        });

    });

}

function edit(id) {
    console.log(id, "-----------")
    sessionStorage.setItem("userId", id);
    xadmin.open('编辑', 'member-edit.html', 600, 400);
}

/*用户-停用*/
function member_stop(obj, id, enable) {
    layer.confirm('确认要' + (enable == 1 ? '停用' : '启用') + '吗？', function (index) {
        isLogin();
        $.ajax({
            url: '/back/user/update',
            data: {id: id, enable: enable == 1 ? 0 : 1},
            dataType: 'json',
            type: 'post',
            success: function (data) {
                if (data.msg == 'success') {
                    if ($(obj).attr('title') == '启用') {
                        //发异步把用户状态进行更改
                        $(obj).attr('title', '停用')
                        $(obj).find('i').html('&#xe62f;');

                        $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                        layer.msg('已停用!', {icon: 5, time: 1000});

                    } else {
                        $(obj).attr('title', '启用')
                        $(obj).find('i').html('&#xe601;');

                        $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                        layer.msg('已启用!', {icon: 6, time: 1000});
                    }
                } else {
                    layer.msg('修改失败', {icon: 5, time: 1000});
                }

            }
        })


    });
}


